package com.shu.utils;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtils {
	
	private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class);

	/**
     * 发送HttpGet请求
     * @param url
     * @return
     */
    public static String sendGet(String url) {
    	CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            	HttpEntity entity = response.getEntity();
            	throw new RuntimeException(EntityUtils.toString(entity));
            }else {
            	//4.处理结果，这里将结果返回为字符串
    	        HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "utf-8");  
            }
        } catch (Exception e) {
        	throw new RuntimeException(e);
        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (Exception e) {
                LOG.error(e.getMessage());
            }
        }
    }
    
    /**
     * PUT 提交参数
     * @param url
     * @param contents
     * @return
     */
    public static String httpDelete(String url,String contents) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();     
        HttpDelete httpDelete = new HttpDelete(url);  
        //设置header  
        httpDelete.setHeader("Content-type", "application/json");      
        CloseableHttpResponse  response = null;    
        try {    
            //响应信息  
        	response = closeableHttpClient.execute(httpDelete);    
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            	HttpEntity entity = response.getEntity();
            	throw new RuntimeException(EntityUtils.toString(entity));
            }else {
            	//4.处理结果，这里将结果返回为字符串
    	        HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "utf-8");  
            }
        } catch (Exception e) {    
        	throw new RuntimeException(e);
        }finally{    
            try {    
            	response.close();    
                closeableHttpClient.close();  //关闭连接、释放资源    
            } catch (Exception e) {    
                e.printStackTrace();    
            }    
        }    
	}
    
    /**
     * PUT 提交参数
     * @param url
     * @param contents
     * @return
     */
    public static String httpPut(String url,String contents) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();     
        HttpPut httpput = new HttpPut(url);  
        //设置header  
        httpput.setHeader("Content-type", "application/json");      
        CloseableHttpResponse  response = null;    
        try {    
        	StringEntity stringEntity = new StringEntity(contents,"utf-8");    
        	httpput.setEntity(stringEntity);    
            //响应信息  
        	response = closeableHttpClient.execute(httpput);    
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            	HttpEntity entity = response.getEntity();
            	throw new RuntimeException(EntityUtils.toString(entity));
            }else {
            	//4.处理结果，这里将结果返回为字符串
    	        HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "utf-8");  
            }
        } catch (Exception e) {    
        	throw new RuntimeException(e);
        }finally{    
            try {    
            	response.close();    
                closeableHttpClient.close();  //关闭连接、释放资源    
            } catch (Exception e) {    
                e.printStackTrace();    
            }    
        }    
	}
    
    /**
     * 发送不带参数的HttpPost请求
     * @param url
     * @return
     */
    public static String sendPost(String url) {
    	CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            	HttpEntity entity = response.getEntity();
            	throw new RuntimeException(EntityUtils.toString(entity));
            }else {
            	//4.处理结果，这里将结果返回为字符串
    	        HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "utf-8");  
            }
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }finally {
			try {
				response.close();
				httpclient.close();
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}
        }
    }


	 /**
     * 发送不带参数的HttpPost请求
     * @param url
     * @return
     */
    public static String sendPost(String url,String contents) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
        	StringEntity stringEntity = new StringEntity(contents);    
        	Header header = new BasicHeader("Content-Type","application/json");
        	httppost.setHeader(header);
        	httppost.setEntity(stringEntity);    
            //3.执行get请求并返回结果
            response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            	HttpEntity entity = response.getEntity();
            	throw new RuntimeException(EntityUtils.toString(entity));
            }else {
            	//4.处理结果，这里将结果返回为字符串
    	        HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity, "utf-8");  
            }
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }finally {
			try {
				response.close();
				httpclient.close();
			} catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}
        }
    }
	
}
